package com.vlong.town.controller.client.index;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.banner.BannerInfo;
import com.vlong.town.entity.goods.GoodsInfo;
import com.vlong.town.entity.hotel.HotelInfo;
import com.vlong.town.entity.insurance.InsuranceInfo;
import com.vlong.town.entity.townInfo.TownInfo;
import com.vlong.town.service.banner.BannerInfoService;
import com.vlong.town.service.goods.GoodsInfoService;
import com.vlong.town.service.hotel.HotelInfoService;
import com.vlong.town.service.insurance.InsuranceInfoService;
import com.vlong.town.service.townInfo.TownInfoService;
import com.vlong.town.util.data.DataUtil;

/**
 * 客户端控制器
 * @author 51088
 *
 */
@Controller
@RequestMapping("/client/index")
public class IndexClientController extends BaseController{
	

    @Autowired
    private BannerInfoService bannerInfoService;// Banner图信息业务实现类	
    @Autowired
	private TownInfoService townInfoService;//新农村业务接口
    @Autowired
	private GoodsInfoService  goodsInfoService;//一寸一品控制器
    @Autowired
	private HotelInfoService hotelInfoService;//休闲农庄业务接口
    @Autowired
	private InsuranceInfoService  insuranceInfoService;//业务接口

    /**
     * 跳转客户端主页面
     * @return
     */
	@RequestMapping("/main")
	public String index(){
		//轮播图
		List<String>  imageslist=new ArrayList<String>();
		List<BannerInfo> bannerInfoList = this.bannerInfoService.queryList(1, null, null);
		for (BannerInfo bannerInfo : bannerInfoList) {
			String imagesUrl=request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + bannerInfo.getUrl();
			imageslist.add(imagesUrl);
		}
		//新农村
		Long townId = 1L;
	    TownInfo townInfo = this.townInfoService.query(townId);
        request.setAttribute("townInfo", townInfo);
		request.setAttribute("imageslist", imageslist);
		PageHelper.startPage(getPageNo(1), getPageSize(5));
		PageHelper.orderBy("create_time DESC");
		//一村一品
		List<GoodsInfo> goodsInfoList = this.goodsInfoService.queryList(null, null, null, null,null);
		for(GoodsInfo goodsInfo : goodsInfoList) {
        	goodsInfo.setThumbnail(request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + goodsInfo.getThumbnail());
            goodsInfo.setContent(DataUtil.delHTMLTag(goodsInfo.getContent(), 0));
            Long createTime = goodsInfo.getCreateTime();
        	SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
        	Date date = new  Date(createTime);
        	String format = sdf.format(date);
        	goodsInfo.setCreateDate(format);
        }
		GoodsInfo goodsInfo = goodsInfoList.get(0);
		request.setAttribute("goodsInfo", goodsInfo);
		goodsInfoList.remove(goodsInfo);
		request.setAttribute("goodsInfoList", goodsInfoList);
		//休闲农庄
		List<HotelInfo> hotelInfoList = this.hotelInfoService.queryList(null, null,null, null,2);
		for(HotelInfo hotelInfo : hotelInfoList) {
        	hotelInfo.setThumbnail(request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + hotelInfo.getThumbnail());
            hotelInfo.setContent(DataUtil.delHTMLTag(hotelInfo.getContent(), 0));
            Long createTime = hotelInfo.getCreateTime();
        	SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
        	Date date = new  Date(createTime);
        	String format = sdf.format(date);
        	hotelInfo.setCreateDate(format);
        }
		HotelInfo hotelInfo = hotelInfoList.get(0);
		request.setAttribute("hotelInfo", hotelInfo);
		hotelInfoList.remove(hotelInfo);
		request.setAttribute("hotelInfoList", hotelInfoList);
		//农牧保险
		List<InsuranceInfo> insuranceList = insuranceInfoService.queryList(null, null, null, null);
		for(InsuranceInfo insuranceInfo : insuranceList) {
        	insuranceInfo.setThumbnail(request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + insuranceInfo.getThumbnail());
        	insuranceInfo.setContent(DataUtil.delHTMLTag(insuranceInfo.getContent(), 0));
            Long createTime = insuranceInfo.getCreateTime();
        	SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
        	Date date = new  Date(createTime);
        	String format = sdf.format(date);
        	insuranceInfo.setCreateDate(format);
        }
		InsuranceInfo insuranceInfo = insuranceList.get(0);
		request.setAttribute("insuranceInfo", insuranceInfo);
		insuranceList.remove(insuranceInfo);
		request.setAttribute("insuranceList", insuranceList);
		//获取SESSION信息
		String telphone = getTelphone();
		request.setAttribute("telphone", telphone);
		return "client/common/index";
	}
}
